﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using Medianamik.Core.Configuration;
using Medianamik.Core;
using System.Configuration;

namespace Medianamik.UI.Lock
{
    public class LockLink : HyperLink
    {
        #region Fields

        private const string IMAGE_CONTROL_ID = "image";

        #endregion

        #region Events

        protected override void OnInit(EventArgs e)
        {
            var image = new Image()
            {
                ID = IMAGE_CONTROL_ID
            };

            Controls.Add(image);

            base.OnInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            var image = (Image)FindControl(IMAGE_CONTROL_ID);

            if (!Enabled)
            {
                image.ImageUrl = DisabledImageUrl;
            }
            else if (IsLocked)
            {
                image.ImageUrl = LockedImageUrl;
            }
            else
            {
                image.ImageUrl = EnabledImageUrl;
            }

            base.OnPreRender(e);
        }

        #endregion

        #region Properties

        private bool? _isLocked;
        protected bool IsLocked
        {
            get
            {
                if(!_isLocked.HasValue)
                    _isLocked = LockManager.IsLocked(this.ObjectId);

                return _isLocked.GetValueOrDefault();
            }
        }

        public bool LockEnabled
        {
            get
            {
                return ViewState["LockEnabled"] as bool? ??
                    GlobalLockEnabled;
            }
            set
            {
                ViewState["LockEnabled"] = value && GlobalLockEnabled;
            }
        }

        protected virtual string DefaultEnabledImageUrl
        {
            get
            {
                return "/Medianamik/images/Icones/Ico_Edition.gif";
            }
        }
        public string EnabledImageUrl
        {
            get
            {
                return ViewState["EnabledImageUrl"] as string ?? DefaultEnabledImageUrl;
            }
            set
            {
                ViewState["EnabledImageUrl"] = value;
            }
        }

        protected virtual string DefaultDisabledImageUrl
        {
            get
            {
                return "/Medianamik/images/Icones/Ico_EditionInactif.gif";
            }
        }
        public string DisabledImageUrl
        {
            get
            {
                return ViewState["DisabledImageUrl"] as string ?? DefaultDisabledImageUrl;
            }
            set
            {
                ViewState["DisabledImageUrl"] = value;
            }
        }

        protected virtual string DefaultLockedImageUrl
        {
            get
            {
                return "/Medianamik/images/Icones/Ico_EditionInactif.gif";
            }
        }
        public string LockedImageUrl
        {
            get
            {
                return ViewState["LockedImageUrl"] as string ?? DefaultLockedImageUrl;
            }
            set
            {
                ViewState["LockedImageUrl"] = value;
            }
        }

        public Guid ObjectId
        {
            get
            {
                return (Guid?)ViewState["ObjectId"] ?? Guid.Empty;
            }
            set
            {
                ViewState["ObjectId"] = value;
            }
        }

        private static bool GlobalLockEnabled
        {
            get
            {
                return ((MedianamikConfigSection)ConfigurationManager.
                    GetSection(ConfigurationSectionName.MEDIANAMIK)).EnableLock;
            }
        }

        #endregion

        #region Methods

        

        #endregion
    }
}
